package com.k.kpp.config.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * @author kivil
 */
public class FtpClientFactory extends BasePooledObjectFactory<FTPClient> {
    private FtpClientProperties config;

    public FtpClientFactory(FtpClientProperties config) {
        System.out.println("ftp.client:" + config.toString());
        this.config = config;
    }

    @Override
    public FTPClient create() throws Exception {
        FTPClient client = new FTPClient();
        client.setControlEncoding(config.getEncoding());
        client.setDataTimeout(config.getDataTimeout());
        client.setConnectTimeout(config.getConnectTimeout());
        client.setControlKeepAliveTimeout(config.getKeepAliveTimeout());
        client.connect(config.getHost(),config.getPort());
        return client;
    }

    @Override
    public PooledObject<FTPClient> wrap(FTPClient ftpClient) {
        return new DefaultPooledObject<FTPClient>(ftpClient);
    }
}
